package com.hibase.core.rule.web.ruletable;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.rule.constant.RuleFieldType;
import com.hibase.core.rule.entity.ruletable.RuleTableField;
import com.hibase.core.rule.entity.ruletable.vo.*;
import com.hibase.core.rule.service.ruletable.RuleTableFieldService;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 动态规则字段表 前端控制器
 * </p>
 *
 * @author hufeng
 * @since 2019-06-10
 */
@RestController
@RequestMapping("/rule-table-field")
@Api(tags = "动态规则字段表")
public class RuleTableFieldController{

    @Autowired
    private RuleTableFieldService ruleTableFieldService;

    @ApiOperation(value = "查询动态规则字段表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectRuleTableFieldList")
    public ResponseModel<RuleTableFieldListVO> selectRuleTableFieldList(@RequestBody RuleTableFieldSearchVO searchVO) {

        PageInfo result = ruleTableFieldService.selectRuleTableFieldList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), RuleTableFieldListVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "新增动态规则字段表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertRuleTableField")
    public ResponseModel<String> insertRuleTableField(@RequestBody RuleTableFieldInsertVO insertVO) {

        RuleTableField entity = EntityUtil.transform(insertVO, RuleTableField.class);

        ruleTableFieldService.saveRuleTableField(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "修改动态规则字段表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updateRuleTableField")
    public ResponseModel<String> updateRuleTableField(@RequestBody RuleTableFieldUpdateVO updateVO) {

        RuleTableField entity = EntityUtil.transform(updateVO, RuleTableField.class);

        ruleTableFieldService.saveRuleTableField(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "查询动态规则字段表详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewRuleTableFieldDetails")
    public ResponseModel<RuleTableFieldViewVO> viewRuleTableFieldDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transformByJson(ruleTableFieldService.viewRuleTableField(operationidEntity.getId()), RuleTableFieldViewVO.class));
    }

    @ApiOperation(value = "删除动态规则字段表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deleteRuleTableField")
    public ResponseModel<String> deleteRuleTableField(@RequestBody OperationIdsEntity delete) {

        ruleTableFieldService.deleteRuleTableField(delete.getIds());

        return ResponseModel.successMsg();
    }
    @ApiOperation(value = "上移", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/moveUp")
    public ResponseModel<String> moveUp(@RequestBody OperationIdEntity id) {

        ruleTableFieldService.fieldSort(id.getId(), RuleFieldType.FIELD_MOVE_UP.getKey());

        return ResponseModel.successMsg();
    }
    @ApiOperation(value = "下移", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/moveDown")
    public ResponseModel<String> moveDown(@RequestBody OperationIdEntity id) {

        ruleTableFieldService.fieldSort(id.getId(), RuleFieldType.FIELD_MOVE_DOWN.getKey());

        return ResponseModel.successMsg();
    }

}
